import socket
import threading

# lock=threading.Lock()
def test(tcp_server_client,ip_port):

    print('ip',ip_port)
    while True:
        recv_data = tcp_server_client.recv(1024).decode('gbk')
        if recv_data:
            print('来自{}的消息：'.format(ip_port),recv_data)
            tcp_server_client.send('收到'.encode('gbk'))
        else:
            print('{}断开'.format(ip_port))
            break



# tcp_server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# tcp_server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
# tcp_server.bind(("",8080))
# tcp_server.listen(128)
# tcp_server_socket,ip_port=tcp_server.accept()
# print('ip',ip_port)
# while True:
#     recv_data=tcp_server_socket.recv(1024).decode('gbk')
#     print('消息',recv_data)
#     tcp_server_socket.send('收到'.encode('gbk'))
# tcp_server_socket.close()
# tcp_server.close()

if __name__ == '__main__':
    tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    tcp_server.bind(("", 8080))
    tcp_server.listen(128)
    while True:
        tcp_server_client, ip_port = tcp_server.accept()
        t=threading.Thread(target=test,args=(tcp_server_client,ip_port))

        t.start()